<template>
  <div class="login-container">
    <div class="login-content">
      <h2>DOSS直聘｜新一代数字化招聘平台</h2>
      <el-form :model="ruleForm" status-icon label-width="auto" class="demo-ruleForm">
        <el-form-item label="账号" prop="name">
          <el-input v-model="ruleForm.name" type="text" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input v-model="ruleForm.password" type="password" autocomplete="off" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm()">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import "element-plus/theme-chalk/src/message.scss";
import microApp from '@micro-zoe/micro-app';
import { reactive } from 'vue';
import { ElMessage } from 'element-plus';
import { login } from "@/api/login";
import { useRouter } from "vue-router"

const router = useRouter();
const ruleForm = reactive({
  name: '',
  password: '',
})

const submitForm = () => {
  login<{ accessToken: string }>(ruleForm).then((res) => {
    if (res.success) {
      ElMessage.success(res.errMsg || "登录成功")
      const { accessToken = "" } = res.data;
      if (accessToken) {
        microApp.setGlobalData({
          token: accessToken
        })
      }
      router.push("/");
    } else {
      ElMessage.error(res.errMsg || "登录失败")
    }
  })
}
</script>

<style>
.login-container {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.login-content {
  width: 50%;
  min-width: 360px;

  >h2 {
    text-align: center;
    margin: 20px;
  }
}
</style>
